﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace HobSoft.SoccerBet2014.Wcf
{
    using Domain.IRepositories;
    using Domain.Models;
    using Domain.ClientService;
    public class BfCustomDataService : IBfCustomDataService
    {
        private readonly IBFEventRepository bfEventRepository;
        private readonly IBFMarketRepository bfMarketRepository;
        private readonly IGameMappingRepository gameMappingRepository;
        public BfCustomDataService(IGameMappingRepository _gameMappingRepository
                                                , IBFEventRepository _bfeventRepository
                                                , IBFMarketRepository _bfMarketRepository)
        {
            bfEventRepository = _bfeventRepository;
            gameMappingRepository = _gameMappingRepository;
            bfMarketRepository = _bfMarketRepository;
        }

        public IList<BFEvent> GetLive(DateTime? lastUpdateTime)
        {
            IList<BFEvent> result = new List<BFEvent>();
            var afterTime = lastUpdateTime.HasValue ? lastUpdateTime.Value : DateTime.Now.AddMinutes(-150);
            var beforeTime = DateTime.Now.AddDays(5);
            var mapping = gameMappingRepository.GetMany(afterTime, beforeTime);
            if (!mapping.Any()) return result;
            var eventIds = mapping.Select(m => m.EventID).ToArray();
            result = bfEventRepository.GetMany(be => eventIds.Contains(be.EventID) && (!be.NotAvailable.HasValue || !be.NotAvailable.Value));
            return result;
        }

        public IList<BFMarket> GetLiveMarket(DateTime? lastUpdateTime)
        {
            IList<BFMarket> result = new List<BFMarket>();
            var afterTime = lastUpdateTime.HasValue ? lastUpdateTime.Value : DateTime.Now.AddMinutes(-150);
            var beforeTime = DateTime.Now.AddDays(5);
            var mapping = gameMappingRepository.GetMany(afterTime, beforeTime);
            if (!mapping.Any()) return result;
            var eventIds = mapping.Select(m => m.EventID).ToArray();
            result = bfMarketRepository.GetMany(m => eventIds.Contains(m.EventID));
            return result;
        }

        public IList<GameMapping> GetMapping(DateTime? lastUpdateTime)
        {
            var afterTime = lastUpdateTime.HasValue ? lastUpdateTime.Value : DateTime.Now.AddMinutes(-150);
            var beforeTime = DateTime.Now.AddDays(5);
            var mapping = gameMappingRepository.GetMany(afterTime, beforeTime);
            return mapping;
        }

        public BFEvent GetEvent(int eventId)
        {
            return bfEventRepository.Get(eventId);
        }

        public IList<BFMarket> GetMarkets(int eventId)
        {
            return bfMarketRepository.GetMany(m => m.EventID == eventId);
        }

        public IList<BFEvent> GetEventByMarket(string[] marketIds)
        {
            IList<BFEvent> result = new List<BFEvent>();
            if (marketIds == null || !marketIds.Any()) return result;
            var markets = bfMarketRepository.GetMany(m => marketIds.Contains(m.MarketID));
            var eventIds = markets.Select(m => m.EventID).ToArray();
            result = bfEventRepository.GetMany(be => eventIds.Contains(be.EventID));
            return result;
        }
    }
}
